﻿Imports DotNetNuke.Web.Services
Imports System.Web.Mvc
Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Content.Common
Imports DotNetNuke.Entities.Host
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.Security
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Services.Social.Notifications
Imports DotNetNuke.Services.Journal

Namespace DotNetNuke.Modules.ActiveForums
    Public Class ModerationServiceController
        Inherits DnnController
        Private TabId As Integer = -1
        Private ModuleId As Integer = -1
        Private ForumId As Integer = -1
        Private TopicId As Integer = -1
        Private ReplyId As Integer = -1
        <DnnAuthorize()> _
        Public Function ApprovePost(notificationId As Integer) As ActionResult
            Dim notify As Notification = NotificationsController.Instance.GetNotification(notificationId)
            ParseKey(notify.Context)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, True)
            If fi Is Nothing Then
                Return Json(New With {.Result = "error"})
            End If

            If Not IsMod(ForumId) Then
                Return Json(New With {.Result = "error"})
            End If
            If ReplyId > 0 Then
                Dim rc As New ReplyController
                Dim reply As ReplyInfo = rc.ApproveReply(PortalSettings.PortalId, TabId, ModuleId, ForumId, TopicId, ReplyId)
                If reply Is Nothing Then
                    Return Json(New With {.Result = "error"})
                End If

            Else
                Dim tc As New TopicsController
                Dim topic As TopicInfo = tc.ApproveTopic(PortalSettings.PortalId, TabId, ModuleId, ForumId, TopicId)
                If topic Is Nothing Then
                    Return Json(New With {.Result = "error"})
                End If

            End If
            NotificationsController.Instance().DeleteNotification(notificationId)
            Return Json(New With {.Result = "success"})
        End Function
        <DnnAuthorize()> _
        Public Function RejectPost(notificationId As Integer) As ActionResult
            Dim notify As Notification = NotificationsController.Instance.GetNotification(notificationId)
            ParseKey(notify.Context)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, True)
            If fi Is Nothing Then
                Return Json(New With {.Result = "error"})
            End If
            If Not IsMod(ForumId) Then
                Return Json(New With {.Result = "error"})
            End If
            Dim mc As New ModController
            mc.Mod_Reject(PortalSettings.PortalId, ModuleId, UserInfo.UserID, ForumId, TopicId, ReplyId)
            Dim authorId As Integer = -1
            If ReplyId > 0 Then
                Dim rc As New ReplyController
                Dim reply As ReplyInfo = rc.Reply_Get(PortalSettings.PortalId, ModuleId, TopicId, ReplyId)

                If reply Is Nothing Then
                    Return Json(New With {.Result = "error"})
                End If
                authorId = reply.Content.AuthorId
            Else
                Dim tc As New TopicsController
                Dim topic As TopicInfo = tc.Topics_Get(PortalSettings.PortalId, ModuleId, TopicId)
                If topic Is Nothing Then
                    Return Json(New With {.Result = "error"})
                End If
                authorId = topic.Content.AuthorId
            End If
            If fi.ModRejectTemplateId > 0 And authorId > 0 Then
                Dim uc As New DotNetNuke.Entities.Users.UserController
                Dim ui As DotNetNuke.Entities.Users.UserInfo = uc.GetUser(PortalSettings.PortalId, authorId)
                If Not ui Is Nothing Then
                    Dim au As New Author
                    au.AuthorId = authorId
                    au.DisplayName = ui.DisplayName
                    au.Email = ui.Email
                    au.FirstName = ui.FirstName
                    au.LastName = ui.LastName
                    au.Username = ui.Username
                    Dim oEmail As New Email
                    oEmail.SendEmail(fi.ModRejectTemplateId, PortalSettings.PortalId, ModuleId, TabId, ForumId, TopicId, ReplyId, String.Empty, au)
                End If

            End If
            NotificationsController.Instance().DeleteNotification(notificationId)
            Return Json(New With {.Result = "success"})
        End Function
        <DnnAuthorize()> _
        Public Function DeletePost(notificationId As Integer) As ActionResult
            Dim notify As Notification = NotificationsController.Instance.GetNotification(notificationId)
            ParseKey(notify.Context)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, True)
            If fi Is Nothing Then
                Return Json(New With {.Result = "error locating forum"})
            End If
            If Not IsMod(ForumId) Then
                Return Json(New With {.Result = "error user not authorized"})
            End If
            Dim authorId As Integer = -1
            If fi.ModDeleteTemplateId > 0 Then
                Try
                    Dim oEmail As New Email
                    oEmail.SendEmail(fi.ModDeleteTemplateId, PortalSettings.PortalId, ModuleId, TabId, ForumId, TopicId, ReplyId, String.Empty, Nothing)
                Catch ex As Exception

                End Try

            End If
            Dim ms As SettingsInfo = DataCache.MainSettings(ModuleId)
            If ReplyId > 0 And ReplyId <> TopicId Then
                Dim rc As New ReplyController
                Dim reply As ReplyInfo = rc.Reply_Get(PortalSettings.PortalId, ModuleId, TopicId, ReplyId)

                If reply Is Nothing Then
                    Return Json(New With {.Result = "error"})
                End If
                authorId = reply.Content.AuthorId
                rc.Reply_Delete(PortalSettings.PortalId, ForumId, TopicId, ReplyId, ms.DeleteBehavior)


            Else
                Dim tc As New TopicsController
                Dim topic As TopicInfo = tc.Topics_Get(PortalSettings.PortalId, ModuleId, TopicId)
                If topic Is Nothing Then
                    Return Json(New With {.Result = "error"})
                End If
                authorId = topic.Content.AuthorId
                tc.Topics_Delete(PortalSettings.PortalId, ModuleId, ForumId, TopicId, ms.DeleteBehavior)

            End If
            NotificationsController.Instance().DeleteNotification(notificationId)
            Return Json(New With {.Result = "success"})

        End Function
        <DnnAuthorize()> _
        Public Function IgnorePost(notificationId As Integer) As ActionResult
            Dim notify As Notification = NotificationsController.Instance.GetNotification(notificationId)
            ParseKey(notify.Context)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, True)
            If fi Is Nothing Then
                Return Json(New With {.Result = "error"})
            End If
            If Not IsMod(ForumId) Then
                Return Json(New With {.Result = "error"})
            End If

            NotificationsController.Instance().DeleteNotification(notificationId)
            Return Json(New With {.Result = "success"})

        End Function
#Region "Private Methods"
        Private Function IsMod(ForumId As Integer) As Boolean
            Dim mods As List(Of UserInfo) = Utilities.GetListOfModerators(PortalSettings.PortalId, ForumId)
            For Each u As UserInfo In mods
                If UserInfo.UserID = u.UserID Then
                    Return True
                    Exit For
                End If
            Next
            Return False
        End Function
        Private Sub ParseKey(key As String)
            Dim keys() As String = key.Split(CChar(":"))
            TabId = Integer.Parse(keys(0))
            ModuleId = Integer.Parse(keys(1))
            ForumId = Integer.Parse(keys(2))
            TopicId = Integer.Parse(keys(3))
            ReplyId = Integer.Parse(keys(4))
        End Sub
#End Region
    End Class
End Namespace

